<template>
  <div class="app-container">

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <!--查询表单-->
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="objectQuery.title" placeholder="输入标题"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="resetData()">清空</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="add()">添加</el-button>
        </el-form-item>

      </el-form>
    </div>

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 20%;margin-right: 20%">
      <!-- 表格 -->
      <el-table :data="list" border stripe style="padding: 30px 0; text-align: center">
        <el-table-column prop="title" label="标题" width="300" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>

        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="openDialog(scope.row.id)">编辑
            </el-button>

            <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20]"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="fetchData"
      @size-change="changePageSize"/>

    <!-- 编辑弹窗-->
    <el-dialog
      title="翻译题目"
      :visible.sync="dialogVisible"
      width="50%"
      style="text-align: center">

      <el-form :inline="true">
        <el-form-item>
          标题<el-input v-model="write.title" placeholder="输入标题"/>
          题目<el-input type="textarea" :rows="4" v-model="write.content" placeholder="输入内容"/>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="danger" @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import writeApi from '@/api/write'

  export default {
    // 页内数据
    data() {
      return {
        list: [],
        page: 1,
        limit: 5,
        total: 0,
        addTitle: "",
        subject: {},
        dialogVisible: false,
        objectQuery: {},
        write: {}
      }
    },

    //初始化
    created() {
      this.objectQuery.type = 2;
      this.fetchData();
    },

    methods: {
      //获取数据
      fetchData(page = 1) {
        this.page = page;
        writeApi.getList(this.page, this.limit, this.objectQuery)
          .then(response => {
            this.list = response.data.rows;
            this.total = response.data.total
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 改变每页记录数
      changePageSize(size) {
        this.limit = size;
        this.fetchData()
      },

      // 重置表单
      resetData() {
        this.objectQuery.title = null;
        this.fetchData()
      },

      // 添加分类
      add() {
        this.write = {}
        this.dialogVisible = true
      },

      // 取消编辑
      cancel() {
        this.dialogVisible = false;
        this.write = {}
      },

      // 删除记录
      remove(id) {
        // 询问是否删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return writeApi.delete(id)
        }).then(response => {
          // 刷新页面
          this.fetchData();
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
        })
      },

      //打开对话框
      openDialog(id) {
        this.dialogVisible = true;
        writeApi.getWrite(id)
          .then(response => {
            this.write = response.data.item
          })
          .catch(error => {
            console.log(error)
          })
      },

      //更新分类
      update() {
        if (this.write.id == null) {
          this.write.type = this.objectQuery.type;
          writeApi.add(this.write)
            .then(response => {
              this.dialogVisible = false;
              this.fetchData();
              this.write = {}
            })
            .catch(error => {
              console.log(error)
            })
        } else {
          writeApi.update(this.write)
            .then(response => {
              this.dialogVisible = false;
              this.fetchData();
              this.write = {}
            })
            .catch(error => {
              console.log(error)
            })
        }
      }
    }
  }
</script>
